package view.GUI.sidepanels;

import java.awt.Color;
import java.util.Observable;
import java.util.Observer;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import model.graphic.objects.AbstractInPort;
import model.graphic.objects.AbstractOutPort;
import model.graphic.objects.GConnection;
import model.graphic.objects.GNode;
import model.graphic.objects.Graphical;
import model.graphic.objects.RectangleAttachment;

/**
 *
 * @author Jelle, modified by Ben
 */
public class PropertiesPanel extends JPanel implements Observer {

    private Graphical g;
    private PropertiesState state = new PropertiesStateGraphical();

    public PropertiesPanel() {
        this.setBackground(Color.WHITE);
        this.setOpaque(true);
    }

    public void adjust() {
        this.removeAll();
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.setAutoscrolls(true);
        String[] columnNames = {"Name", "Value"};
        PropertiesTable table = new PropertiesTable(new PropertiesTableModel(columnNames, getTableMatrix(), g),state);
        add(table);
        this.updateUI();
    }

    public Object[][] getTableMatrix() {
        return state.getTableMatrix(g);
    }

    public void update(Observable o, Object arg) {
        if (arg != null && Graphical.class.isAssignableFrom(arg.getClass())) {
            this.g = (Graphical) arg;
             state = new PropertiesStateGraphical();
            if (GNode.class.isAssignableFrom(g.getClass())) {
                if (((GNode) g).getValue() != null) {
                    g = (GNode) g;
                    state = new PropertiesStateGNode();
                }
            }else if(GConnection.class.isAssignableFrom(g.getClass())){
                g = (GConnection)g;
                state = new PropertiesStateGConnection();
            }else if(AbstractInPort.class.isAssignableFrom(g.getClass())){
                g = (RectangleAttachment)g;
                state = new PropertiesStateAbstractInPort();
            }else if(AbstractOutPort.class.isAssignableFrom(g.getClass())){
                g = (RectangleAttachment)g;
                state = new PropertiesStateAbstractOutPort();
            }
            adjust();
        }
    }
}
